﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace Qins.PluginLoader
{
    public class DirControl
    {
        public static void CreateDirectory(string dir)
        {
            if (OperatingSystem.IsWindows()) // standard guard examples
            {
                DirectoryInfo info = new DirectoryInfo(dir);
                if (!info.Exists)
                {
                    DirectorySecurity directorySecurity = new DirectorySecurity();
                    directorySecurity.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
                    directorySecurity.AddAccessRule(new FileSystemAccessRule("Administrators", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
                    directorySecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
                    directorySecurity.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
                    info.Create(directorySecurity);
                    info.Attributes = FileAttributes.Normal;
                }
            }
            else
            {
                Directory.CreateDirectory(dir);
            }            
        }

        public static void DeleteFile(string file)
        {
            try
            {
                if (File.Exists(file))
                {
                    File.SetAttributes(file, FileAttributes.Normal);
                    File.Delete(file);
                }
            }
            catch
            {
            }
        }

        public static void DeleteFolderRecursive(DirectoryInfo dir)
        {
            if (dir.Exists)
            {
                foreach (FileInfo info in dir.GetFiles())
                {
                    try
                    {
                        info.Attributes = FileAttributes.Normal;
                        info.Delete();
                    }
                    catch
                    {
                    }
                }
                foreach (DirectoryInfo info2 in dir.GetDirectories())
                {
                    DeleteFolderRecursive(info2);
                }
                try
                {
                    dir.Attributes = FileAttributes.Normal;
                    dir.Delete();
                }
                catch
                {
                }
            }
        }

        public static long GetDirectoryLength(string dirPath)
        {
            if (!Directory.Exists(dirPath))
            {
                return 0L;
            }
            long num = 0L;
            DirectoryInfo info = new DirectoryInfo(dirPath);
            foreach (FileInfo info2 in info.GetFiles())
            {
                num += info2.Length;
            }
            DirectoryInfo[] directories = info.GetDirectories();
            if (directories.Length > 0)
            {
                for (int i = 0; i < directories.Length; i++)
                {
                    num += GetDirectoryLength(directories[i].FullName);
                }
            }
            return num;
        }
    }
}
